package com.crack.nga2.localimage;


import android.graphics.Bitmap;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.crack.nga2.R;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class SelectImageFragment extends Fragment implements View.OnClickListener {
    private GridView gridView;
    private List<String> imagePathList = new ArrayList<>();
    private List<String> imageSelected = new ArrayList<>();
    private NativeImageLoader imageLoader;
    private TextView textView;
    private Button btnSend;

    public SelectImageFragment() {
        // Required empty public constructor
    }

    public SelectImageFragment(List<String> imagePathList) {
        this.imagePathList = imagePathList;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_select_image, container, false);
        imageLoader = NativeImageLoader.getInstance();
        gridView = (GridView) rootView.findViewById(R.id.gridView);
        gridView.setAdapter(new ImageAdapter());
        textView = (TextView) rootView.findViewById(R.id.select_count);
        btnSend = (Button) rootView.findViewById(R.id.send);
        btnSend.setOnClickListener(this);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        gridView = null;
        textView = null;
        btnSend = null;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.select_state:
                //右上角imageView
                ImageView selectImage = (ImageView) v;
                //对应的显示图片的imageView
                ImageView imageView = (ImageView) selectImage.getTag();
                String path = (String) imageView.getTag();
                boolean state = imageSelected.contains(path);
                if(state){
                    imageSelected.remove(path);
                    selectImage.setImageResource(R.drawable.image_unselected_icon);
                }else {
                    imageSelected.add(path);
                    selectImage.setImageResource(R.drawable.selected);
                }
                int count = imageSelected.size();
                textView.setText(String.valueOf(count));
                break;
            case R.id.send:
                if(imageSelected.size()==0){
                    return;
                }
                SendCallBack callBack = (SendCallBack) getActivity();
                callBack.add(imageSelected);
                break;
        }
    }

    public interface SendCallBack{
        void add(List<String> pathList);
    }

    private class ImageAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return imagePathList.size();
        }

        @Override
        public Object getItem(int position) {
            return imagePathList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View view, final ViewGroup parent) {
            final ImageView imageView;
            ImageView selectState;
            if (view == null) {
                view = View.inflate(getContext(), R.layout.post_select_image_item, null);
                imageView = (ImageView) view.findViewById(R.id.select_image);
                view.setTag(imageView);
            } else {
                imageView = (ImageView) view.getTag();
            }
            selectState = (ImageView) view.findViewById(R.id.select_state);
            selectState.setTag(imageView);
            selectState.setOnClickListener(SelectImageFragment.this);
            String path = imagePathList.get(position);
            imageView.setTag(path);
            boolean state = imageSelected.contains(path);
            selectState.setImageResource(state ? R.drawable.selected : R.drawable.image_unselected_icon);
            Bitmap bitmap = imageLoader.loadNativeImage(path, new Point(95, 95), new NativeImageLoader.NativeImageCallBack() {
                @Override
                public void onImageLoader(Bitmap bitmap, String path) {
                    if (imageView.getTag() == path && bitmap != null) {
                        imageView.setImageBitmap(bitmap);
                    }
                }
            });
            if (bitmap != null) {
                imageView.setImageBitmap(bitmap);
            }
            return view;
        }
    }

}
